package com.cw.jeeyt.controller.security;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.cw.jeeyt.api.dto.response.JsonResultDto;
import com.cw.jeeyt.common.enums.EnumPermissionAssignType;
import com.cw.jeeyt.common.web.BaseController;
import com.cw.jeeyt.service.bo.security.SecurityResourceBo;
import com.cw.jeeyt.service.intf.security.SecurityResourceService;
import com.cw.jeeyt.service.intf.security.SecurityService;

/**
 * 权限管理控制类
 *
* @author chenw
* @version 1.0.0
 */
@Controller
@RequestMapping("/security")
public class SecurityController extends BaseController {
    private static Logger logger = LoggerFactory.getLogger(SecurityController.class);
    @Resource
    private SecurityResourceService securityResourceService;
    @Resource
    private SecurityService securityService;

    /**
     * 跳转到权限分配tab页面
     *
     * @return
     */
    @RequestMapping(value = "/to-securityAssign-tab")
    public ModelAndView toSecurityAssignTab() {
        ModelAndView mv = new ModelAndView("security/securityAssign_tab");
        return mv;
    }

    /**
     * 跳转到角色或用户权限分配页面
     *
     * @return
     */
    @RequestMapping(value = "/to-securityAssign")
    public ModelAndView toSecurityAssign(String type) {
        ModelAndView mv = null;
        //用户权限分配
        if (type.equals(EnumPermissionAssignType.USER.getCode())) {
            mv = new ModelAndView("security/securityAssign_user");
            logger.info("跳转到用户权限分配页面");
            //角色权限分配
        } else if (type.equals(EnumPermissionAssignType.ROLE.getCode())) {
            mv = new ModelAndView("security/securityAssign_role");
            logger.info("跳转到角色权限分配页面");
        }
        return mv;
    }

    /**
     * 跳转到角色或用户权限分配页面
     *
     * @return
     */
    @RequestMapping(value = "/to-securityAssign-dialog")
    public ModelAndView securityAssignDialog(String type, Long id) throws Exception {
        ModelAndView mv = new ModelAndView("security/securityAssign_form");
        //加载用户、角色菜单树
        //加载根节点菜单信息
        SecurityResourceBo securityResource = securityResourceService.queryRootSecurityResource();
        String nodesJson = securityResourceService.queryResourceJson(securityResource.getId(), type, id, true, true);
        if (!StringUtils.isEmpty(nodesJson)) {
            nodesJson = nodesJson.replace("\"", "'");
        }
        mv.addObject("nodesJson", nodesJson);
        mv.addObject("type", type);
        mv.addObject("assignId", id);
        return mv;
    }

    /**
     * 分配权限
     */
    @ResponseBody
    @RequestMapping(value = "/security_assign", method = RequestMethod.POST)
    public JsonResultDto securityAssign(String ids, Long assignId, String type) {
        JsonResultDto jsonResultDto = new JsonResultDto(false);
        try {
            securityService.assignSecurity(ids, assignId, type);
            jsonResultDto.setSuccess(true);
            jsonResultDto.setMsg("权限分配成功");
        } catch (Exception e) {
            e.printStackTrace();
            jsonResultDto.setMsg("权限分配失败：" + e.getMessage());
            logger.error("权限分配失败：{}", e.getMessage());
        }
        return jsonResultDto;
    }
}
